package model

import (
	"gorm.io/gorm"
	"iqiyi-srv/basic/config"
	__ "iqiyi-srv/basic/proto"
	"time"
)

type User struct {
	Id           uint32         `gorm:"column:id;type:int UNSIGNED;comment:ID;primaryKey;not null;" json:"id"`                     // ID
	Mobile       string         `gorm:"column:mobile;type:char(11);comment:账号;not null;" json:"mobile"`                          // 账号
	Password     string         `gorm:"column:password;type:char(32);comment:密码;not null;" json:"password"`                      // 密码
	Nickname     string         `gorm:"column:nickname;type:varchar(60);comment:昵称;not null;" json:"nickname"`                   // 昵称
	Image        string         `gorm:"column:image;type:varchar(300);comment:头像;not null;default:默认头像;" json:"image"`       // 头像
	ProtobufTime time.Time      `gorm:"column:protobuf_time;type:datetime(3);comment:会员过期时间;not null;" json:"protobuf_time"` // 会员过期时间
	CreatedAt    time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt    time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt    gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (u *User) TableName() string {
	return "user"
}

// 注册
func (u *User) CreateUser() error {
	return config.DB.Create(&u).Error
}

// 账号获取用户
func (u *User) MobileGetUser() error {
	return config.DB.Where("mobile = ?", u.Mobile).Find(&u).Error
}

// ID获取用户
func (u *User) IDGetUser() error {
	return config.DB.Where("id = ?", u.Id).Find(&u).Error
}

type UserVip struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:ID;primaryKey;not null;" json:"id"`                                             // ID
	Name      string         `gorm:"column:name;type:varchar(60);comment:套餐名字;not null;" json:"name"`                                               // 套餐名字
	VipCate   int8           `gorm:"column:vip_cate;type:tinyint(1);comment:时长规格 0-一个月 1-三个月 2-十二个月;not null;default:0;" json:"vip_cate"` // 时长规格 0-一个月 1-三个月 2-十二个月
	Info      string         `gorm:"column:info;type:varchar(60);comment:权益说明;not null;" json:"info"`                                               // 权益说明
	Price     int32          `gorm:"column:price;type:int;comment:原价;not null;" json:"price"`                                                         // 原价
	PriceNew  int32          `gorm:"column:price_new;type:int;comment:现价;not null;" json:"price_new"`                                                 // 现价
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (v *UserVip) nTableName() string {
	return "user_vip"
}

// 套餐
func (v *UserVip) AddUserVip() error {
	return config.DB.Create(&v).Error
}

func (v *UserVip) GetUserVip() (list []*__.UserVipList, err error) {
	err = config.DB.Model(UserVip{}).
		Select("name,vip_cate,info,price,price_new").
		Find(&list).Error
	return
}

type UserLike struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:ID;primaryKey;not null;" json:"id"` // ID
	VideoId   int32          `gorm:"column:video_id;type:int;comment:视频ID;not null;" json:"video_id"`     // 视频ID
	UserId    int32          `gorm:"column:user_id;type:int;comment:用户ID;not null;" json:"user_id"`       // 用户ID
	CreatedAy time.Time      `gorm:"column:created_ay;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_ay"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (l *UserLike) TableName() string {
	return "user_like"
}

// 添加
func (l *UserLike) AddLike() error {
	return config.DB.Create(&l).Error
}

func (l *UserLike) GetUserLike(userId int64) (list []*__.GetUserLikeVideo, err error) {
	err = config.DB.Model(UserLike{}).
		Select("video_id,user_id").
		Where("user_id = ?", userId).
		Find(&list).Error
	return
}

type UserVideoLog struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:ID;primaryKey;not null;" json:"id"`      // ID
	UserId    int32          `gorm:"column:user_id;type:int;comment:用户ID;not null;" json:"user_id"`            // 用户ID
	VideoId   int32          `gorm:"column:video_id;type:int;comment:视频ID;not null;" json:"video_id"`          // 视频ID
	LookTime  time.Time      `gorm:"column:look_time;type:datetime;comment:播放位置;not null;" json:"look_time"` // 播放位置
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (l *UserVideoLog) TableName() string {
	return "user_video_log"
}

// 添加
func (l *UserVideoLog) AddUserVideoLog() error {
	return config.DB.Create(&l).Error
}
